﻿// -----------------------------------------------------------------------
// <copyright file="DateData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db Date
    /// </summary>
    public class DateData
    {
        /// <summary>
        /// Get Date By Id
        /// </summary>
        /// <param name="dateId">Param dateId</param>
        /// <returns>Return Date</returns>
        public DateEN GetDateById(int dateId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                DateEN date = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneDateGetById, dateId);
                while (reader.Read())
                {
                    date = new DateEN();

                    if (!reader.IsDBNull(0))
                    {
                        date.DateId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        date.Name = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        date.Value = reader.GetInt32(2);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return date;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all Date
        /// </summary>
        /// <returns>Return list all Date</returns>
        public IList<DateEN> GetDates()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<DateEN> lstDate = new List<DateEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneDateGetAll);
                while (reader.Read())
                {
                    DateEN date = new DateEN();

                    if (!reader.IsDBNull(0))
                    {
                        date.DateId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        date.Name = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        date.Value = reader.GetInt32(2);
                    }

                    lstDate.Add(date);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstDate;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db Date
        /// </summary>
        /// <param name="date">Paarm Date</param>
        /// <returns>Insert Date</returns>
        public int Insert(DateEN date)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Date.Name, date.Name),
                                    new SqlParameter(Constants.SqlParamerterName.Date.Value, date.Value)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneDateInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db Date
        /// </summary>
        /// <param name="date">Param Date</param>
        public void Update(DateEN date)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Date.DateId, date.DateId),
                                    new SqlParameter(Constants.SqlParamerterName.Date.Name, date.Name),
                                    new SqlParameter(Constants.SqlParamerterName.Date.Value, date.Value)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneDateUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete Date by dateId
        /// </summary>
        /// <param name="dateId">param dateId</param>
        public void Delete(int dateId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.Date.DateId, dateId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneDateDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}